package com.tec.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tec.security.entity.ViewsIP;
import com.tec.security.mapper.ViewsIPMapper;
import com.tec.security.service.ViewsIPService;
import com.tec.security.utils.date.DateString;
import com.tec.security.utils.other.GainIP;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
* @author LuLin
* @description 针对表【ViewsIP】的数据库操作Service实现
* @createDate 2023-10-21 09:22:51
*/
@Service
public class ViewsIPServiceImpl extends ServiceImpl<ViewsIPMapper,ViewsIP> implements ViewsIPService {
    private final ViewsIPMapper ViewsIPMapper;
    @Autowired
    public ViewsIPServiceImpl(com.tec.security.mapper.ViewsIPMapper viewsIPMapper) {
        ViewsIPMapper = viewsIPMapper;
    }

    public int allIP(){
        QueryWrapper<ViewsIP> premiumVisitorsQueryWrapper=new QueryWrapper<>();
        premiumVisitorsQueryWrapper.select("ip");
        return Math.toIntExact(ViewsIPMapper.selectCount(premiumVisitorsQueryWrapper));
    }

    public int todayIP(HttpServletRequest request) {
        ViewsIP ViewsIP=new ViewsIP();
        ViewsIP.setTime(DateString.currentTime());
        ViewsIP.setDate(DateString.currentDate());
        ViewsIP.setIp(GainIP.getIpAddress(request));
        QueryWrapper<ViewsIP> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("date",ViewsIP.getDate()).eq("ip",ViewsIP.getIp());
        List<ViewsIP> List=ViewsIPMapper.selectList(queryWrapper);
        if(List.isEmpty()){
            ViewsIPMapper.insert(ViewsIP);
            return List.size()+1;
        }else{
            return List.size();
        }
    }
    public int todayIP_count(){
        QueryWrapper<ViewsIP> ViewsIPQueryWrapper=new QueryWrapper<>();
        ViewsIPQueryWrapper.eq("date",DateString.currentDate());
        return Math.toIntExact(ViewsIPMapper.selectCount(ViewsIPQueryWrapper));
    }

    @Override
    public int todayIP_gain() {
        QueryWrapper<ViewsIP> ViewsIPQueryWrapper=new QueryWrapper<>();
        ViewsIPQueryWrapper.eq("date",DateString.currentDate());
        return Math.toIntExact(ViewsIPMapper.selectCount(ViewsIPQueryWrapper));
    }
}




